import {
    ICtrlActionResult,
	IParam,
	IPickupGridViewAbility,
	IPickupTreeViewAbility,
	IPickupViewPanelAbility,
	IPickupViewPanelController,
	IPickupViewPanelControllerParams,
	IPickupViewPanelModel,
	IPickupViewPanelStore,
	IViewAbility,
	PickupViewPanelActionType,
} from '@/core/interface';
import { DECtrlController } from './de-ctrl-controller';

/**
 * 选择视图面板部件控制器（todo：注释+能力）
 *
 * @export
 * @class PickupViewPanelController
 * @extends {DECtrlController<PickupViewActionType, IPickupViewPanelStore, IPickupViewPanelAbility>}
 */
export class PickupViewPanelController
	extends DECtrlController<
		PickupViewPanelActionType,
		IPickupViewPanelStore,
		IPickupViewPanelAbility
	>
	implements
		IPickupViewPanelController<IPickupViewPanelStore, IPickupViewPanelAbility>
{
	/**
	 * 选择视图面板模型
	 *
	 * @type {IPickupViewPanelModel}
	 * @memberof PickupViewPanelController
	 */
	declare model: IPickupViewPanelModel;

	/**
	 * Creates an instance of PickupViewPanelController.
	 * @param {IPickupViewPanelControllerParams<PickupViewPanelActionType, IPickupViewPanelAbility>} params
	 * @memberof PickupViewPanelController
	 */
	public constructor(
		params: IPickupViewPanelControllerParams<
			PickupViewPanelActionType,
			IPickupViewPanelAbility
		>
	) {
		super(params);
		this.ctrlInit(params);
	}

	/**
	 * 获取嵌入视图能力
	 *
	 * @return {*}  {(IPickupGridViewAbility | IPickupTreeViewAbility | undefined)}
	 * @memberof PickupViewPanelController
	 */
	public getEmbedViewAbility():
		| IPickupGridViewAbility
		| IPickupTreeViewAbility
		| undefined {
		if (this.model.embedViewCodeName) {
			return this.getSubAbility<
				IPickupGridViewAbility | IPickupTreeViewAbility
			>(this.model.embedViewCodeName);
		}
		return undefined;
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	public handleCtrlAction(name: string, action: string, data: IParam[]): void {
		super.handleCtrlAction(name, action, data);
		if (this.model.embedViewCodeName === name) {
			this.handleEmbedViewAction(action, data);
		}
	}

	/**
	 * 处理嵌入视图事件
	 *
	 * @param {string} action
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	public handleEmbedViewAction(action: string, data: IParam[]) {
		switch (action) {
			case 'viewDataChange':
				this.handleValueChange(data);
				break;
			case 'viewDataActive':
				this.handleDataActive(data);
				break;
		}
	}

	/**
	 * 处理数据变化
	 *
	 * @private
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	private handleValueChange(data: IParam[]) {
		this.emit('selectionChange', data);
	}

	/**
	 * 处理数据激活
	 *
	 * @private
	 * @param {IParam[]} data
	 * @memberof PickupViewPanelController
	 */
	private handleDataActive(data: IParam[]) {
		this.emit('dataActive', data);
	}

    /**
     * 加载数据
     *
     * @param {IParam} [opts={}]
     * @return {*}  {Promise<ICtrlActionResult>}
     * @memberof PickupViewPanelController
     */
    public async load(opts: IParam = {}): Promise<ICtrlActionResult>{
        const ability = this.getEmbedViewAbility();
        if (ability) {
            return ability.load(opts);
        }
        return { ok: false, data: this.getData(), rowData: { status: 500 } };
    }


	/**
	 * 选中全部
	 *
	 * @memberof PickupViewPanelController
	 */
	public selectAll(): void {
		const ability = this.getEmbedViewAbility();
		if (ability) {
			ability.selectAll();
		}
	}

	/**
	 * 选中数据
	 *
	 * @param {IParam[]} items 需要选中的数据
	 * @param {boolean} [reverse] 是否反选
	 * @memberof PickupViewPanelController
	 */
	public selectItems(items: IParam[], reverse?: boolean): void {
		const ability = this.getEmbedViewAbility();
		if (ability) {
			ability.selectItems(items, reverse);
		}
	}

	/**
	 * 获取能力
	 *
	 * @return {*}  {IPickupViewPanelAbility}
	 * @memberof PickupViewPanelController
	 */
	getAbility(): IPickupViewPanelAbility {
		return {
			...super.getAbility(),
            load: this.load.bind(this),
			selectAll: this.selectAll.bind(this),
			selectItems: this.selectItems.bind(this),
		};
	}
}
